/*
 * Copyright (C)1995-2009 Elcom Software
 */
package com.elcom.emocbc.util;

import java.io.FileInputStream;
import java.io.InputStream;
import java.util.Properties;
import java.util.concurrent.ConcurrentHashMap;

/**
 *
 * @author Nguyen Thua Thang
 *
 */
public class ConfigurationManager {

    public static final String HOME_DIR = System.getProperty("user.home");
    public static final String SEPARATOR = System.getProperty("file.separator");
    private static final String CONFIG_FOLDER = "config/cellbroadcast";
    private static final String DIR = HOME_DIR + SEPARATOR + CONFIG_FOLDER + SEPARATOR;
    private static ConfigurationManager instance = new ConfigurationManager();
    private static final ConcurrentHashMap<String, Properties> cachedProperties = new ConcurrentHashMap<String, Properties>();

    private ConfigurationManager() {
    }

    public synchronized static ConfigurationManager getInstance() {
        if (instance == null){
            instance = new ConfigurationManager();
        }
        return instance;
    }

    public synchronized Properties getProperties(String filename) {
        if (!cachedProperties.containsKey(filename)) {
            Properties loaded = loadProperties(filename);
            cachedProperties.put(filename, loaded);
        }
        return cachedProperties.get(filename);
    }

    public synchronized static void reloadConfiguration() {
        cachedProperties.clear();
    }

    public synchronized void reloadConfiguration(String module) {
        cachedProperties.remove(module);
    }

    private synchronized Properties loadProperties(String module) {
        Properties properties = new Properties();
        String filename = DIR + module.toLowerCase() + ".properties";
        String classpathFile = CONFIG_FOLDER + SEPARATOR + module.toLowerCase() + ".properties";
        try {
            FileInputStream pin = new FileInputStream(filename);
            properties.load(pin);
            return properties;
        } catch (Exception e) {
        }
        try {
            InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream(classpathFile);
            properties.load(inputStream);
        } catch (Exception e) {
        }
        return properties;
    }

    public synchronized static String getStrProp(String fileName, String key, String defaultValue) {
        Properties properties = instance.getProperties(fileName);
        return properties.getProperty(key, defaultValue);
    }

    public synchronized static int getIntProp(String fileName, String key, int defaultValue) {
        Properties properties = instance.getProperties(fileName);
        return Integer.parseInt(properties.getProperty(key, String.valueOf(defaultValue)));
    }

    public synchronized static long getLongProp(String fileName, String key, long defaultValue) {
        Properties properties = instance.getProperties(fileName);
        return Long.parseLong(properties.getProperty(key, String.valueOf(defaultValue)));
    }

    public synchronized static double getDoubleProp(String fileName, String key, double defaultValue) {
        Properties properties = instance.getProperties(fileName);
        return Double.parseDouble(properties.getProperty(key, String.valueOf(defaultValue)));
    }

    public synchronized static byte getByteProp(String fileName, String key, byte defaultVal) {
        Properties properties = instance.getProperties(fileName);
        return Byte.parseByte(properties.getProperty(key, Byte.toString(defaultVal)));
    }

    public synchronized static boolean getBooleanProp(String fileName, String key, boolean defaultVal) {
        Properties properties = instance.getProperties(fileName);
        return Boolean.parseBoolean(properties.getProperty(key, String.valueOf(defaultVal)));
    }
}
